﻿from django.contrib.auth.models import AnonymousUser
from django.core.cache import cache
from userprofile.models import UserProfile
from yazasport import settings

class CacheProfileMiddleware(object):
    def process_request(self, request):
        if request.user != AnonymousUser():
            cache_key = 'profile_for_%s' % (request.user.id)
            request.profile = cache.get(cache_key)
            if request.profile is None:
                request.profile = UserProfile.objects.select_related().get(user__id__exact=request.user.id)
                cache.set(cache_key, request.profile)
        return None